from time import time

def binstr(n):
    d = { "0": "000", "1": "001", "2": "010", "3": "011",
          "4": "100", "5": "101", "6": "110", "7": "111" }
    return "".join([d[c] for c in oct(n)]).lstrip("0")
    
limit = 1000 * 1000
sum = 0

start = time()

for i in xrange(limit):
    s = str(i)
    if s == s[::-1]:
        b = binstr(i)
        if (b == b[::-1]):
            sum += i
            
print sum
print "Time: ", time() - start